package org.itboys.core.service.mongo.front;

import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.coupon.SundayCouponMapper;
import org.itboys.core.dao.coupon.SundayCouponReceiveMapper;
import org.itboys.core.dto.coupon.SundayCouponDto;
import org.itboys.core.entity.mongo.SundayCouponSubstract;
import org.itboys.core.entity.mongo.SundayCouponUserArea;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mysql.SundayCoupon;
import org.itboys.core.entity.mysql.SundayCouponReceive;
import org.itboys.core.service.mongo.web.SundayCouponSubstractService;
import org.itboys.core.service.mongo.web.SundayCouponUserAreaService;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.Sun;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/29
 * @Description：优惠券领用
 */
@Service
public class SundayFrontCouponService extends BaseMySqlService<SundayCouponReceive>{
    @Autowired
    private SundayCouponReceiveMapper couponReceiveMapper;
    @Autowired
    private SundayCouponMapper couponMapper;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayCouponUserAreaService couponUserAreaService;
    @Autowired
    private SundayCouponSubstractService couponSubstractService;

   public List<SundayCouponDto> select(HttpServletRequest request,Long memberId,Integer state){
       Map<String, Object> param = ParamUtil.packageMysqlPageParam(request);
       param.put("memberId",memberId);
       param.put("isDeleted",SundayCommonConstants.NO);
       if(state.equals(SundayCommonConstants.COUPON_STATE_RECEIVE_YES)){
           List<SundayCouponReceive> couponReceives = couponReceiveMapper.select(param);
           if(!ListUtil.isNotNull(couponReceives)){
               return null;
           }
           return packageDtos(couponReceives);
       }else if(SundayCommonConstants.COUPON_STATE_USED.equals(state)){
           //筛选出已使用的
           param.put("used",SundayCommonConstants.YES);
           List<SundayCouponReceive> couponReceives = couponReceiveMapper.select(param);
           if(!ListUtil.isNotNull(couponReceives)){
               return null;
           }
           return packageDtos(couponReceives);
       }else if(SundayCommonConstants.COUPON_STATE_OUTDATE.equals(state)){
           //查询出已过期的优惠券
           List<SundayCoupon> coupons = couponMapper.selectOutDate();
           List<Long> couponIds=new ArrayList<>();
           for (SundayCoupon c:coupons
                ) {
               couponIds.add(c.getId());
           }
           param.put("couponIds",couponIds);
           List<SundayCouponReceive> couponReceives = couponReceiveMapper.select(param);
           if(!ListUtil.isNotNull(couponReceives)){
               return null;
           }
           return packageDtos(couponReceives);
       }else if(SundayCommonConstants.COUPON_STATE_RECEIVE_NO.equals(state)){
            //获取用户身份,获取符合用户范围的优惠券
           SundayMember member = memberService.getById(memberId);
           Integer userType = member.getUserType();
           //获取相应用户类型的优惠券
           Map<String,Object> _param=new HashMap<>();
           _param.put("userType",userType);
           _param.put("isDeleted",SundayCommonConstants.NO);
           List<SundayCouponUserArea> couponUserAreas=new ArrayList<>();
           couponUserAreas = couponUserAreaService.list(_param);
           List<Long> couponIds=new ArrayList<>();
           for (SundayCouponUserArea c:couponUserAreas
                ) {
               couponIds.add(c.getCouponId());
           }
           //获取使用所有用户类型的优惠券
           _param.clear();
           _param.put("userType",SundayCommonConstants.user_type_all);
           couponUserAreas=couponUserAreaService.list(_param);
           for (SundayCouponUserArea c:couponUserAreas
                   ) {
               couponIds.add(c.getCouponId());
           }
           //获取用户已经领取过的优惠券id
           List<SundayCouponReceive> couponReceives = couponReceiveMapper.select(param);
           List<Long> receiveIds=new ArrayList<>();
           for (SundayCouponReceive c:couponReceives
                ) {
               receiveIds.add(c.getCouponId());
           }
           //排除掉用户已经领取的优惠券
           couponIds.removeAll(receiveIds);
           if(!ListUtil.isNotNull(couponIds)){
              return null;
           }
           param.put("couponIds",couponIds);
           List<SundayCoupon> sundayCoupons = couponMapper.selectCanReceive(param);
           return packageDtoBySundayCoupon(sundayCoupons);
       }
       return null;
   }

   public List<SundayCouponDto> packageDtos(List<SundayCouponReceive> couponReceives){
       List<Long> couponIds=new ArrayList<>();
       for (SundayCouponReceive couponReceive:couponReceives
               ) {
           couponIds.add(couponReceive.getCouponId());
       }
       Map<String,Object> param=new HashMap<>();
       param.put("isDeleted",SundayCommonConstants.NO);
       param.put("ids",couponIds);
       List<SundayCoupon> coupons = couponMapper.select(param);
        return packageDtoBySundayCoupon(coupons);
   }
   public List<SundayCouponDto> packageDtoBySundayCoupon(List<SundayCoupon> coupons){
       List<SundayCouponDto> result=new ArrayList<>();
       List<Long> ids=new ArrayList<>();
       for (SundayCoupon c:coupons
            ) {
           ids.add(c.getId());
       }
       Map<String,Object> param=new HashMap<>();
       param.put("isDeleted",SundayCommonConstants.NO);
       param.put("couponId in",ids);
       List<SundayCouponSubstract> substracts = couponSubstractService.list(param);
       for (SundayCoupon c:coupons
               ) {
           SundayCouponDto couponDto=new SundayCouponDto();
           couponDto.setId(c.getId());
           couponDto.setName(c.getName());
           couponDto.setEndDate(c.getEndDate());
           couponDto.setState(c.getState());
           for (SundayCouponSubstract substract:substracts
                ) {
                if(c.getId().equals(substract.getCouponId())){
                    couponDto.setBaseAmount(substract.getBaseAmount());
                    couponDto.setSubAmount(substract.getSubAmount());
                    break;
                }
           }
           result.add(couponDto);
       }
       return  result;
   }
}
